from machine import I2C, Pin
from ssd1306 import SSD1306_I2C
import framebuf


oled = None

def test_font_file():
	from YouYuan__size_24__chars_9 import Font

	font = Font()
	pos_x = pos_y = 20
	
	oled.fill(0)

	# test_char_list = ['0xe788b1', '0xefbc8c', '0xe4b8ad', '0xefbc9f'] # 爱，中？
	test_char_list = ['0xe788b1', '0xe68891', '0xe4b8ad', '0xe58d8e'] # 爱我中华
	for char in test_char_list:
		'''
		glyph 格式
			1. glyph[0]: bbx
				bbx[0]: width
				bbx[1]: height
				bbx[2]: offset_x
				bbx[3]: offset_y
			2. glyph[1]: dwidth
			3. glyph[2]: buffer
		'''
		glyph = font.get_glyph_utf8(char)

		buffer = glyph[2]
		bbx = glyph[0]
		bbx_width = bbx[0]
		bbx_height = bbx[1]
		bbx_offset_x = bbx[2]
		bbx_offset_y = bbx[3]

		buffer = framebuf.FrameBuffer(buffer, bbx_width, bbx_height, framebuf.MONO_VLSB)

		offset_x = font.offset_x + bbx_offset_x
		offset_y = font.height + font.offset_y - bbx_height - bbx_offset_y
		
		oled.blit(buffer, pos_x + offset_x, pos_y + offset_y)

		# print('font\n    width: {}\n    height: {}'.format(font.width, font.height))
		# oled.rect(pos_x, pos_y, font.width, font.height, 1)

		pos_x += font.width - 1
	
	oled.show()


def main():
	global oled

	i2c = I2C(0, scl=Pin(18), sda=Pin(19))
	slave_list = i2c.scan()

	if slave_list:
		print('slave id: {}'.format(slave_list[0]))
		oled = SSD1306_I2C(128, 64, i2c)

		test_font_file()

if __name__ == "__main__":
	main()
